//
//  SJSettingCell.m
//  WSL
//
//  Created by CJ on 2018/2/1.
//  Copyright © 2018年 sanjuetech. All rights reserved.
//

#import "SJSettingCell.h"


@interface SJSettingCell ()

@property (strong, nonatomic) UIView *bgView;
@property (strong, nonatomic) UIImageView *rightImageView;
@property (strong, nonatomic) UILabel *detailLabel;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UIButton *displayButton;
@property (strong, nonatomic) UILabel *logoutLabel;

@end

@implementation SJSettingCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setItem:(NSDictionary *)item {
    _item = item;
    _titleLabel.text = item[@"title"];
    _detailLabel.text = item[@"detail"];
    
    if ([item[@"title"] isEqualToString:@"显示标题"]) {
        
        _logoutLabel.hidden = YES;
        _displayButton.hidden = NO;
        _titleLabel.hidden = NO;
        _detailLabel.hidden = NO;
        _rightImageView.hidden = YES;
        
    } else if ([item[@"title"] isEqualToString:@"退出登录"]) {
        
        _logoutLabel.hidden = NO;
        _displayButton.hidden = YES;
        _titleLabel.hidden = YES;
        _detailLabel.hidden = YES;
        _rightImageView.hidden = YES;
        
    } else {
        
        _logoutLabel.hidden = YES;
        _displayButton.hidden = YES;
        _titleLabel.hidden = NO;
        _detailLabel.hidden = NO;
        _rightImageView.hidden = NO;
    }
    
    if ([item[@"title"] isEqualToString:@"性别"]) {
        
        NSString *gender = nil;
        if ([item[@"detail"] isEqualToString:@"1"]) {
            gender = @"男";
        } else {
            gender = @"女";
        }
        _detailLabel.text = gender;
    }
    
    if ([item[@"title"] isEqualToString:@"手机号"]) {
        
        if ([item[@"detail"] isEqualToString:@""]) {
            _detailLabel.text = @"未绑定";
        } else {
            _detailLabel.text = item[@"detail"];
        }
    }

}


- (void)setupUI {
    
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.bgView];
    self.bgView.sd_layout
    .leftSpaceToView(self.contentView, SJMargin)
    .rightSpaceToView(self.contentView, SJMargin)
    .bottomSpaceToView(self.contentView, 0)
    .heightIs(SJItemWH);
    
    [self.bgView addSubview:self.titleLabel];
    self.titleLabel.sd_layout
    .leftSpaceToView(self.bgView, SJLayoutFrame(14))
    .centerYEqualToView(self.bgView)
    .heightIs(SJItemWH);
    [self.titleLabel setSingleLineAutoResizeWithMaxWidth:SJScreenW / 2];
    
    
    [self.bgView addSubview:self.rightImageView];
    self.rightImageView.sd_layout
    .rightSpaceToView(self.bgView, SJLayoutFrame(20))
    .centerYEqualToView(self.bgView)
    .heightIs(SJLayoutFrame(12))
    .widthIs(SJLayoutFrame(6));
    
    
    [self.bgView addSubview:self.displayButton];
    self.displayButton.sd_layout
    .rightSpaceToView(self.bgView, SJLayoutFrame(20))
    .centerYEqualToView(self.bgView)
    .heightIs(SJLayoutFrame(20))
    .widthIs(SJLayoutFrame(40));
    
    [self.bgView addSubview:self.logoutLabel];
    self.logoutLabel.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
    [self.bgView addSubview:self.detailLabel];
    self.detailLabel.sd_layout
    .rightSpaceToView(self.rightImageView, SJLayoutFrame(10))
    .centerYEqualToView(self.bgView)
    .heightIs(SJItemWH);
    [self.detailLabel setSingleLineAutoResizeWithMaxWidth:SJScreenW / 2];
    
}

#pragma mark - getter

- (UIView *)bgView {
    
    if (!_bgView) {
        
        _bgView = [UIView new];
        _bgView.sd_cornerRadius = @(SJCornerRadius);
        _bgView.backgroundColor = SJColorHexString(@"#ffffff25");
//        _bgView.alpha = .1;
    }
    return _bgView;
}

- (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        
        _titleLabel = [UILabel new];
        _titleLabel.textColor = SJColorHexString(@"#ffffff");
        _titleLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}

- (UILabel *)detailLabel {
    
    if (!_detailLabel) {
        
        _detailLabel = [UILabel new];
        _detailLabel.textColor = SJColorHexString(@"#ffffff");
        _detailLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        _detailLabel.textAlignment = NSTextAlignmentRight;
        _detailLabel.alpha = .6;

    }
    return _detailLabel;
}

- (UILabel *)logoutLabel {
    
    if (!_logoutLabel) {
        
        _logoutLabel = [UILabel new];
        _logoutLabel.textColor = SJColorHexString(@"#99c7ff");
        _logoutLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        _logoutLabel.textAlignment = NSTextAlignmentCenter;
        _logoutLabel.text = @"退出登录";
        
    }
    return _logoutLabel;
}


- (UIImageView *)rightImageView {
    
    if (!_rightImageView) {
        
        _rightImageView = [UIImageView new];
        _rightImageView.image = [UIImage imageNamed:@"personal_icon_Details"];
        
    }
    return _rightImageView;
}

- (UIButton *)displayButton {
    
    if (!_displayButton) {
        
        _displayButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_displayButton setImage:[UIImage imageNamed:@"personal_setting_off"] forState:UIControlStateNormal];
        [_displayButton setImage:[UIImage imageNamed:@"personal_setting_on"] forState:UIControlStateSelected];
        [_displayButton addTarget:self action:@selector(displayAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _displayButton;
}

- (void)displayAction {
    
    
}

@end
